﻿#region GNU GPL Version 3 License

/// Copyright 2013 Shailesh Lolam
/// 
/// This file Cloud.cs is part of AzEzAuto.
/// 
/// AzEzAuto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
/// 
/// AzEzAuto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
/// 
/// You should have received a copy of the GNU General Public License along with AzEzAuto. If not, see http://www.gnu.org/licenses/.
/// 
/// History:
/// ______________________________________________________________
/// Created         04-2013             Shailesh Lolam

#endregion
      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzEzAuto.Entities;
using AzEzAuto.Entities.Hosted;
using System.Security.Cryptography.X509Certificates;
using AzEzAuto.Utils;
using System.Collections.Specialized;

namespace AzEzAuto.Management
{
    public class Cloud : Base
    {
        public Cloud(HttpApi httpApi)
            : base(httpApi)
        {
        }

        public Cloud(HttpApi httpApi, string version)
            : base(httpApi, version)
        {
        }


        public string CreateCloudService(string subscriptionId, CreateHostedService chs)
        {
            Guard.ArgumentNull(chs, "chs");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices", subscriptionId)), "POST", chs, headers);
            return headers[REQUEST_ID];
        }

        public string DeleteCloudService(string subscriptionId, string serviceName)
        {
            Guard.ArgumentNullOrEmpty(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}", subscriptionId, serviceName)), "DELETE", null, headers);
            return headers[REQUEST_ID];
        }

        public string UpdateCloudService(string subscriptionId, UpdateHostedService uhs, string serviceName)
        {
            Guard.ArgumentNull(uhs, "uhs");
            Guard.ArgumentNullOrEmpty(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}", subscriptionId, serviceName)), "PUT", uhs, headers);
            return headers[REQUEST_ID];
        }

        public string CreateDeployment(string subscriptionId, CreateDeployment cd, string serviceName, DeploymentSlot slot)
        {
            Guard.ArgumentNull(cd, "cd");
            Guard.ArgumentNullOrEmpty(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}", subscriptionId, serviceName, slot)), "POST", cd, headers);
            return headers[REQUEST_ID];
        }


        public HostedService GetHostedServiceProperties(string subscriptionId, string serviceName, bool detail = true)
        {
            Guard.ArgumentNullOrEmpty(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            return _httpApi.Get<HostedService>(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}?embed-detail={2}", subscriptionId, serviceName, detail)), headers);
        }

        public string SwapDeployment(string subscriptionId, Swap swap, string serviceName)
        {
            Guard.ArgumentNull(swap, "swap");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}", subscriptionId, serviceName)), "POST", swap, headers);
            return headers[REQUEST_ID];
        }

        public string DeleteDeployment(string subscriptionId, string serviceName, DeploymentSlot slot)
        {
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}", subscriptionId, serviceName, slot)), "DELETE", null, headers);
            return headers[REQUEST_ID];
        }

        public string DeleteDeployment(string subscriptionId, string serviceName, string deploymentName)
        {
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.ArgumentNull(deploymentName, "deploymentName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}", subscriptionId, serviceName, deploymentName)), "DELETE", null, headers);
            return headers[REQUEST_ID];
        }

        public string ChangeDeploymentConfig(string subscriptionId, ChangeConfiguration cc, string serviceName, DeploymentSlot slot)
        {
            Guard.ArgumentNull(cc, "cc");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}/?comp=config", subscriptionId, serviceName, slot)), "POST", cc, headers);
            return headers[REQUEST_ID];
        }

        public string ChangeDeploymentConfig(string subscriptionId, ChangeConfiguration cc, string serviceName, string deploymentName)
        {
            Guard.ArgumentNull(cc, "cc");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.ArgumentNull(deploymentName, "deploymentName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}/?comp=config", subscriptionId, serviceName, deploymentName)), "POST", cc, headers);
            return headers[REQUEST_ID];
        }

        public string UpdateDeploymentStatus(string subscriptionId, UpdateDeploymentStatus uds, string serviceName, DeploymentSlot slot)
        {
            Guard.ArgumentNull(uds, "uds");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}/?comp=status", subscriptionId, serviceName, slot)), "POST", uds, headers);
            return headers[REQUEST_ID];
        }

        public string UpdateDeploymentStatus(string subscriptionId, UpdateDeploymentStatus uds, string serviceName, string deploymentName)
        {
            Guard.ArgumentNull(uds, "uds");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.ArgumentNull(deploymentName, "deploymentName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}/?comp=status", subscriptionId, serviceName, deploymentName)), "POST", uds, headers);
            return headers[REQUEST_ID];
        }

        public string UpgradeDeployment(string subscriptionId, UpgradeDeployment ud, string serviceName, DeploymentSlot slot)
        {
            Guard.ArgumentNull(ud, "ud");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}/?comp=upgrade", subscriptionId, serviceName, slot)), "POST", ud, headers);
            return headers[REQUEST_ID];
        }

        public string UpgradeDeployment(string subscriptionId, UpgradeDeployment ud, string serviceName, string deploymentName)
        {
            Guard.ArgumentNull(ud, "ud");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.ArgumentNull(deploymentName, "deploymentName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}/?comp=upgrade", subscriptionId, serviceName, deploymentName)), "POST", ud, headers);
            return headers[REQUEST_ID];
        }

        public string WalkUpgradeDomain(string subscriptionId, WalkUpgradeDomain wud, string serviceName, DeploymentSlot slot)
        {
            Guard.ArgumentNull(wud, "wud");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}/?comp=walkupgradedomain", subscriptionId, serviceName, slot)), "POST", wud, headers);
            return headers[REQUEST_ID];
        }

        public string WalkUpgradeDomain(string subscriptionId, WalkUpgradeDomain wud, string serviceName, string deploymentName)
        {
            Guard.ArgumentNull(wud, "wud");
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.ArgumentNull(deploymentName, "deploymentName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}/?comp=walkupgradedomain", subscriptionId, serviceName, deploymentName)), "POST", wud, headers);
            return headers[REQUEST_ID];
        }
        
        
        public string RollbackDeployment(string subscriptionId, string serviceName)
        {
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/?comp=rollback", subscriptionId, serviceName)), "DELETE", null, headers);
            return headers[REQUEST_ID];
        }

        public string RollbackDeployment(string subscriptionId, string serviceName, string deploymentName)
        {
            Guard.ArgumentNull(serviceName, "serviceName");
            Guard.ArgumentNull(deploymentName, "deploymentName");
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            _httpApi.Post(new Uri(string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}/?comp=rollback", subscriptionId, serviceName, deploymentName)), "DELETE", null, headers);
            return headers[REQUEST_ID];
        }
    }
}
